﻿using CHS.ChengDu.Service.Dto;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CHS.ChengDu.Service
{
    public class SCGSService
    {
        static System.Type yh_chs = Type.GetTypeFromProgID("YinHai.CHS.InterfaceSCS");
        static object yh_chs_obj;

        #region yh_interface_init
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataType"></param>
        /// <returns></returns>
        public SCGSOutput Init(string dataType="1")
        {
            int int_appcode = -1;
            string message = "";

            if (yh_chs_obj == null)
            {
                yh_chs_obj = Activator.CreateInstance(yh_chs);
            }

            object[] args = new object[] { dataType, int_appcode, message };

            ParameterModifier pm = new ParameterModifier(2);
            pm[0] = true;
            pm[1] = true;
            ParameterModifier[] pmd = { pm };
            try
            {
                //调用接口
                yh_chs.InvokeMember("yh_interface_init",
                BindingFlags.InvokeMethod,
                null,
                yh_chs_obj,
                args,
                pmd,
                System.Globalization.CultureInfo.CurrentCulture,
                null);
                int_appcode = int.Parse(args[1].ToString());
                message = args[1].ToString();
                return new SCGSOutput
                {
                    result = message,
                    code = int_appcode
                };
            }
            catch (Exception e)
            {
                return new SCGSOutput
                {
                    result = e.Message,
                    code = -1
                };
            }

        }
        #endregion

        # region Call 
        /// <summary>
        /// 入参DataType可选1或2，默认值为1。
        /// 1：接口入参和返回数据格式为JSON格式。
        /// 2：接口入参和返回数据格式为XML格式。
        /// 返回值 int_appcode 等于 0 成功，其他失败，message 失败提示
        /// </summary>
        /// <param name="_infno">交易编号 入口参数</param>
        /// <param name="_input">交易输入 入口参数</param>
        /// <param name="_output">交易输出 出口参数</param>
        /// <returns></returns>
        public SCGSOutput Call( SCGSInput input)
        {

            //初始化接口
            var initResult = Init(dataType: "1");

            if (!initResult.IsSuccess)
            {
                return initResult;
            }

            if (yh_chs_obj == null)
            {
                yh_chs_obj = Activator.CreateInstance(yh_chs);
            }
            string output = "";
            int appcode = 1;
            //交易输入参数
            object[] args = new object[] { input.input, appcode, output };

            ParameterModifier pm = new ParameterModifier(3);
            pm[0] = false;
            pm[1] = true;
            pm[2] = true;
            ParameterModifier[] pmd = { pm };
            try
            {
                yh_chs.InvokeMember("yh_interface_call",
                               BindingFlags.InvokeMethod,
                               null,
                               yh_chs_obj,
                               args,
                               pmd,
                               System.Globalization.CultureInfo.CurrentCulture,
                               null);

                output = args[2].ToString();
                appcode = int.Parse( args[1].ToString());
                //释放函数
                Destroy();
                return new SCGSOutput
                {
                    code = appcode,
                    result = output
                };                
            }
            catch (Exception e)
            {
                
                //释放函数
                Destroy();
                return new SCGSOutput
                {
                    code = -1,
                    result = e.Message
                };
            }
            
        }

        #endregion

        # region yh_CHS_print 
        /// <summary>
        /// 打印报销单据
        /// </summary>
        /// <param name="_input">交易输入 入口参数</param>
        /// <param name="_output">交易输出 出口参数</param>
        /// <returns></returns>

        public void CHS_Pint(string _input, ref string _output)
        {
            //_logInfo.Info("CHS_Pint 开始调用 input:" + _input);
            if (yh_chs_obj == null)
            {
                yh_chs_obj = Activator.CreateInstance(yh_chs);
            }
            //交易输入参数
            object[] args = new object[] { _input, _output };

            ParameterModifier pm = new ParameterModifier(2);
            pm[0] = false;
            pm[1] = true;
            ParameterModifier[] pmd = { pm };
            try
            {
                yh_chs.InvokeMember("yh_CHS_print",
                BindingFlags.InvokeMethod,
                null,
                yh_chs_obj,
                args,
                pmd,
                System.Globalization.CultureInfo.CurrentCulture,
                null);
                _output = args[1].ToString();
                //     _logInfo.Info("CHS_Pint 调用结束 output:" + _output);
            }
            catch (Exception e)
            {
                //      _logInfo.Error(e.Message);
            }
        }
        #endregion

        #region Destroy         
        /// <summary>
        /// 释放接口
        /// </summary>
        public void Destroy()
        {
            if (yh_chs_obj == null)
            {
                yh_chs_obj = Activator.CreateInstance(yh_chs);
            }
            //交易输入参数
            object[] args = new object[] { };

            yh_chs.InvokeMember("yh_interface_destory",
                BindingFlags.InvokeMethod,
                null,
                yh_chs_obj,
                null,
                null,
                System.Globalization.CultureInfo.CurrentCulture,
                null);
            //  _logInfo.Info("CHS_Destroy 释放接口");
        }
        #endregion

    }
}
